package com.shy.base.vo;

import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutorService;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource;

import cn.hutool.core.thread.ThreadUtil;

/**
 * 全局常量信息
 * 
 * @author shanhanyu
 */
public class AppConstants {
	
	/** 初始化一个线程池，用于执行异步任务 */
	public static ExecutorService executorService = ThreadUtil.newExecutor(2);

	/**登录頁面路径*/
    public static final String TIMEOUT_URL = "TIMEOUT_URL";
    
    /** 登录時用戶名字段*/
    public final static String LOGIN_USER_PARAM = "username";
    
    /** 操作日志详情，需要屏蔽部分字段 */
    public final static String[] EXCLUDE_PARAMS_LOG = new String[] {"password"};
    
    /** cookie name */
    public static final String TOKEN = "Authentication";
    
    /**token有效期基數*/
    public static final Integer EXPIRE_BASE = 24 * 60 * 60 * 1000;
    
    /**
	 * 未使用字段過濾
	 */
	public static final String UNUSED_FIELDS = "unusedFields";
	
	/**
	 * like查询字段過濾
	 */
	public static final String LIKE_TYPES = "likeFields";

	/** 参数存儲map對象 */
	public static Map<String, String> configMap = new HashMap<String, String>();
	
	/** 上傳目录格式 */
    public static final String UPLOAD_BASE_PATH = "/uploads";
	
	/** 上傳目录格式 */
	public static final String UPLOAD_PATH_FORMAT = "/{yyyy}/{mm}/{dd}/{time}{rand:6}";
	
	/** 允許上傳图片格式 */
	public static final String[] UPLOAD_ALLOWED_TYPE_IMG = new String[] {".png", ".jpg", ".jpeg", ".gif", ".bmp"};
	
	/** 允許添加水印的图片格式 */
	public static final String[] WATERMARKER_ALLOWED_TYPE_IMG = new String[] {".png", ".jpg", ".jpeg"};
	
	/** 允許上傳静态资源文件格式 */
    public static final String[] UPLOAD_ALLOWED_TYPE_STATIC = new String[] {".png", ".jpg", ".jpeg", 
            ".gif", ".ico", ".js", ".css", ".html", ".ftl", ".svg", ".eot", ".ttf", ".woff"};
    
    /** 临时信息key */
    public static final String REDIS_TEMP_INFO = "temp:";
    
    /** 緩存有效期 24h*/
    public static final long REDIS_24H_EXPIRE = 24 * 60 * 60;
    
    /** 緩存有效期2h */
    public static final long REDIS_2H_EXPIRE = 2 * 60 * 60;
    
    /** 緩存有效期10m */
    public static final long REDIS_10M_EXPIRE = 10 * 60;
    
    /** 緩存有效期5m */
    public static final long REDIS_5M_EXPIRE = 5 * 60;
    
    /** 附件类型图片 */
    public static final String ATTACH_TYPE_IMG = "IMG";
    
    /** 附件类型文件 */
    public static final String ATTACH_TYPE_FILE = "FILE";
    
    /** http请求默认超时时间 */
    public static final int HTTP_TIMEOUT = 10000;
    
    /** 1MB 大小 */
    public static final int MB_SIZE = 1024 * 1024;
    
    
    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
    
    public static final String WEATHER_API = "http://d1.weather.com.cn/weather_index/{}.html";
	public static final String 	REFERER_WEATHER = "http://www.weather.com.cn/";
	public static final String 	WEATHER_API_1D = "http://www.weather.com.cn/weather1d/{}.shtml";
	public static final String 	WEATHER_API_7D = "http://www.weather.com.cn/weather/{}.shtml";
	
	public static final String[] USERAGENT = new String[] {
		"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;",
		"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
		"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
		"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
		"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv,2.0.1) Gecko/20100101 Firefox/4.0.1",
		"Mozilla/5.0 (Windows NT 6.1; rv,2.0.1) Gecko/20100101 Firefox/4.0.1",
		"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
		"Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
		"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
		"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
		"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
		"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",
		"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)",
		"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
		"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)",
		"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)",
		"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
		"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)",
		"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"
	};
    
    
	static {
		try {
			Properties properties = new Properties();
			ClassPathResource classPathResource = new ClassPathResource("parameter.properties");
			InputStream in = classPathResource.getInputStream();
			properties.load(in);
			// 参数
			Set<Entry<Object, Object>> set = properties.entrySet();
			// 返回在此Set中的元素上进行迭代的迭代器
			Iterator<Map.Entry<Object, Object>> it = set.iterator();
			String key = null;
			String value = null;
			// 循环取出key-value
			while (it.hasNext()) {
				Entry<Object, Object> entry = it.next();
				key = String.valueOf(entry.getKey());
				value = String.valueOf(entry.getValue());
				key = key == null ? key : key.trim();
				value = value == null ? value : value.trim();
				//如果數據庫未配置，才從配置文件中讀取
				if(!configMap.containsKey(key) || StringUtils.isEmpty(configMap.get(key))) {
					configMap.put(key, value);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
